#include "videowidget.h"
#include <QComboBox>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QLabel>
#include <QMediaCaptureSession>
#include <QMediaDevices>
#include <QVBoxLayout>
#include "utils/util.h"
videoWidget::videoWidget(QWidget *parent)
    : QWidget{parent} //, vW(new QVideoWidget())
    , scene(new QGraphicsScene())
    , view(new QGraphicsView())
    , MirrorBox(new QCheckBox())
    , noiseReductionBox(new QCheckBox())
    , darkSceneBox(new QCheckBox())
{
    //设置内容的左上右下边距
    this->setContentsMargins(0, 0, 0, 0);
    QVBoxLayout *mainVLayout = new QVBoxLayout(this);
    mainVLayout->setObjectName("mainVLayout");
    mainVLayout->setSpacing(13);

    //定义一个存放view的水平布局
    QHBoxLayout *viewLayout = new QHBoxLayout();
    view->setObjectName("view");
    view->setScene(scene);
    view->setContentsMargins(0, 0, 0, 0);
    view->setSceneRect(140, 80, 200, 290);
    view->setFrameStyle(QFrame::NoFrame);
    view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    viewLayout->addWidget(view);
    mainVLayout->addLayout(viewLayout);

    cameraDevice = QMediaDevices::defaultVideoInput();
    if (!cameraDevice.isNull()) {
        camera = new QCamera(cameraDevice, this);
        //cameraDevice --->(创建摄像头对象)camera---->(将摄像头设置为捕获对象)captureSession--->
        //(将捕获对象输出到videoItem中)videoItem---->(将videoItem放入到场景scene中)scene---->(将场景放入到到视图中)QGraphicsView

        //创建媒体捕捉会话
        if (!captureSession) {
            captureSession = std::make_unique<QMediaCaptureSession>();
            //将摄像头作为输入
            captureSession->setCamera(camera);
            // InitCameraViedo(true);
        }
    } else {
        qDebug() << "cameraDevice is null";
    }
    InitCameraVideo(true);
    QVBoxLayout *vBoxLayout = new QVBoxLayout();
    //设置vBoxLayout的每个组件的间距
    vBoxLayout->setSpacing(6);
    vBoxLayout->setAlignment(Qt::AlignLeft);
    QLabel *label1 = new QLabel();
    label1->setText("摄像头");
    label1->setObjectName("label1");
    QFont font = label1->font();
    font.setFamily("Microsoft YaHei");
    font.setBold(true);
    font.setPointSize(12);
    font.setStyleStrategy(QFont::PreferAntialias);
    label1->setFont(font);
    vBoxLayout->addWidget(label1);

    // this->resize(800, 600);

    //加入一个comboBox
    QComboBox *combox = new QComboBox();
    combox->setObjectName("combox");
    combox->addItem("Integrated Camera");

    // 设置边框样式
    combox->setStyleSheet("QComboBox {"
                          "    border: 1px solid gray;" // 边框宽度和颜色
                          "    border-radius: 5px;"     // 圆角半径
                          "    padding: 1px 18px 1px 3px;" // 内边距，确保文本不与边框重叠
                          "    min-width: 150px;"          // 最小宽度
                          "}"
                          "QComboBox::drop-down {"
                          "subcontrol-position: padding;"
                          "subcontrol-position: top right;"
                          "width: 16px;"
                          "border: none; /* 尺边框 */"
                          "border-image: url(:/resources/down-arrow.png) ;"
                          "}");
    vBoxLayout->addWidget(combox);
    mainVLayout->addLayout(vBoxLayout);
    //mainVLayout->addSpacing(10);
    //再增加一个放置checkBox的按钮的垂直布局
    QVBoxLayout *ckeckBoxVLayout = new QVBoxLayout();
    ckeckBoxVLayout->setObjectName("ckeckBoxVLayout");
    ckeckBoxVLayout->setSpacing(10);
    ckeckBoxVLayout->setAlignment(Qt::AlignLeft);

    //然后就是增加checkBox
    MirrorBox->setObjectName("MirrorBox");
    MirrorBox->setText("视频镜像");
    MirrorBox->setChecked(true);
    QFont font2 = MirrorBox->font();
    setQFont(font2, "Microsoft YaHei", 10, false);
    MirrorBox->setFont(font2);
    ckeckBoxVLayout->addWidget(MirrorBox);

    //然后就是增加视频降噪按钮
    noiseReductionBox->setObjectName("noiseReductionBox");
    noiseReductionBox->setText("视频降噪");
    noiseReductionBox->setFont(font2);
    ckeckBoxVLayout->addWidget(noiseReductionBox);

    //然后就是增加暗场景增强按钮
    darkSceneBox->setObjectName("darkSceneBox");
    darkSceneBox->setText("暗场景增强");
    darkSceneBox->setFont(font2);
    ckeckBoxVLayout->addWidget(darkSceneBox);

    QLabel *spaceLabel = new QLabel();
    spaceLabel->setMinimumSize(QSize(170, 150));
    spaceLabel->setMaximumSize(QSize(170, 150));
    ckeckBoxVLayout->addWidget(spaceLabel);
    mainVLayout->addLayout(ckeckBoxVLayout);

    connect(MirrorBox, &QCheckBox::toggled, this, &videoWidget::onMirrorToogle);
}

videoWidget::~videoWidget() {}

void videoWidget::InitCameraVideo(bool isFlip)
{
    //创建显示视频的图形项
    videoItem = std::make_unique<QGraphicsVideoItem>();
    videoItem->setSize(QSize(482, 445));
    // //将图形项加入到场景中
    scene->addItem(videoItem.get());
    if (isFlip) {
        //进行水平镜像的变换，使用QTransform
        QTransform transform;
        transform.scale(-1, 1);
        videoItem->setTransform(transform);

        //需要调整位置，因为水平翻转是按照y轴不动，将x翻转到负坐标处，类似于翻书的这种
        //此时图形项的原点就编程负的了，因此我们就需要移动位置了
        //获取到图形项的区域
        QRectF rect = videoItem->boundingRect(); //QRectF表示二维空间中的浮点矩形
        videoItem->setPos(rect.width(), 0);
    }
    captureSession->setVideoOutput(videoItem.get());
}

void videoWidget::onMirrorToogle(bool checked)
{
    cameraVideoNoMirror();
    if (checked) {
        InitCameraVideo(true);
    } else {
        InitCameraVideo(false);
    }
}

void videoWidget::showVideo()
{
    //InitCameraVideo(true);
    //启动摄像头
    camera->start();
    //显示视图
    // view->show();
}

void videoWidget::cameraVideoNoMirror()
{
    if (videoItem) {
        videoItem.reset();
    }
}

void videoWidget::stopVideo()
{
    qDebug() << "摄像头已关闭";
    camera->stop();
    emit stopped();
}

bool videoWidget::cameraStatus()
{
    return camera->isActive();
}
